package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    /**
     * 分页查询声音专辑列表
     * @param trackInfoVoPage
     * @param trackInfoQuery
     * @return
     */
    IPage<TrackListVo> selectUserTrackPage(Page<TrackListVo> trackInfoVoPage, @Param("vo") TrackInfoQuery trackInfoQuery);

    /**
     * 修改专辑排序
     * @param albumId
     * @param orderNum
     */
    //mybatis注解，将指定专辑（album_id）中的序号大于当前声音（order_num）的所有声音的序号-1，且只更新未逻辑删除的
    @Update("update track_info set order_num = order_num -1 where album_id = #{albumId} and order_num > #{orderNum} and is_deleted = 0")
    void updateOrderNum(@Param("albumId") Long albumId, @Param("orderNum") Integer orderNum);


    /**
     * 分页查询专辑声音列表
     * @param albumTrackListVoPage
     * @param albumId
     * @return
     */
    @Select("select\n" +
            "    track.id track_id,\n" +
            "    track.track_title,\n" +
            "    track.media_duration,\n" +
            "    track.order_num,\n" +
            "    max(case when stat.stat_type='0701' then stat.stat_num else 0 end) playStatNum,\n" +
            "    max(case when stat.stat_type='0704' then stat.stat_num else 0 end) commentStatNum,\n" +
            "    track.create_time\n" +
            "from track_info track inner join track_stat stat on track.id = stat.track_id\n" +
            "where track.album_id = #{albumId} and track.status = '0501' and track.is_deleted = 0\n" +
            "group by track.id")
    IPage<AlbumTrackListVo> selectAlbumTrackList(Page<AlbumTrackListVo> albumTrackListVoPage, @Param("albumId") Long albumId);

    /**
     * 修改专辑声音播放数
     * @param count
     * @param trackId
     * @param statType
     */
    @Update("update track_stat set stat_num = stat_num+#{count} where track_id = #{trackId} and stat_type = #{statType} and is_deleted = 0")
    int updateTrackStat(Integer count, Long trackId, String statType);
}
